package com.nariano.webapp;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.FormComponentFeedbackBorder;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator;

import com.nariano.server.domain.entities.User;
import com.nariano.server.services.userservice.UserService;
import com.nariano.server.utils.mail.MailSender;
import com.nariano.server.utils.spring.SpringFactory;

public class ForgotPasswordPage extends WebPage {

    private transient static Logger logger = Logger.getLogger(ForgotPasswordPage.class.getCanonicalName());  
    
    private static class Email implements Serializable {
        String email = "";
    }
    
    private Email email = new Email();
    
    
    public ForgotPasswordPage() {
		this(false);
	}
    
    public ForgotPasswordPage(final boolean successfullySent) {
       
        Form form = new Form("form");
        add(form);

        FeedbackPanel feedback = new FeedbackPanel("feedback");
        add(feedback);

        form.add(new FormComponentFeedbackBorder("border2").add(new TextField(
                "email", new PropertyModel(email, "email")).setRequired(true)
                .add(new  PasswordExistsValidator())));


        form.add(new Button("submit",  new ResourceModel("submitButton")) {
            @Override
            public void onSubmit() {
                logger.fine("Email password to " + email);
                sendPassword();
                setResponsePage(new ForgotPasswordPage(true));
            }
        });
        
        add(new Label("passwordSent", new ResourceModel("passwordSentSuccess")){

			@Override
			public boolean isVisible() {
				return successfullySent;
			}});
        
        
        add(new Link("index", null){
            
            @Override
            public boolean isVisible() {
                return true;
            }

            @Override
            public void onClick() {
                setResponsePage(Index.class);
       }});
    }

	private void sendPassword() {
    	MailSender mailSender = SpringFactory.getBean("mailSender", MailSender.class);
        UserService userService = SpringFactory.getBean("userService", UserService.class);

        User user = userService.findUserFromEmail(email.email);  
        mailSender.send(user.getEmail());
    }
    
}

class PasswordExistsValidator extends AbstractValidator {

    @Override
    protected void onValidate(IValidatable validatable) {
        if (SpringFactory.getBean("userService", UserService.class)
                        .findUserFromEmail((String) validatable.getValue()) == null) {
            error(validatable);
        }
    }

    @Override
    protected String resourceKey() {
        return "PasswordExistsValidator";
    }

    @Override
    protected Map variablesMap(IValidatable validatable) {
        Map map = super.variablesMap(validatable);
        map.put("email", validatable.getValue());
        return map;
    }
}



    
